import { useState, useEffect } from 'react';

/**
 * @useSourceStep 文件编制步骤条hooks
 * @returns stepsItems 当前步骤条内容
 * @returns currentStep 步骤条
 * @returns changeStep 修改步骤条
 * @returns setNoShowKeys 设置不展示的步骤条
 */
export const useSourceStep = ({ info, container }: { info: Record<string, any>; container: Record<string, any> }) => {
  // 控制添加寻源步骤条
  const [currentStep, setCurrentStep] = useState(0);

  // 步骤条内容，根据不同包展示不同内容  status: 'finish',status: 'wait',
  const [stepsItems, setStepsItems] = useState<Record<string, any>[]>([
    {
      key: 'source-tender',
      title: '招标公告',
      status: undefined,
    },
    {
      key: 'source-clarify',
      title: '招标文件澄清修改',
      status: undefined,
    },
    {
      key: 'source-experts',
      title: '组建评标委员会',
      status: undefined,
    },
    {
      key: 'source-supplier',
      title: '开评标',
      status: undefined,
    },
    {
      key: 'source-calibrate',
      title: '定标',
      status: undefined,
    },
  ]);

  // 存储不需要展示的步骤条key
  const [noShowKeys, setNoShowKeys] = useState<String[]>(['source-change']);

  // 修改步骤条
  const changeStep = (state: string, step: number = currentStep) => {
    if (state == 'prev') {
      setCurrentStep(currentStep - 1);
    } else if (state == 'next') {
      setCurrentStep(currentStep + 1);
    } else if (state == 'custom') {
      setCurrentStep(step);
    }
  };

  // 根据不同条件设置步骤条内容
  useEffect(() => {
    const newSetpsItems: Record<string, any>[] = [];
    for (let item in container) {
      newSetpsItems.push({
        key: item,
        title: container[item].title,
      });
    }
    let keys: String[] = noShowKeys;
    if (Object.keys(info).length) {
      const { businessTypeName } = info;
      // const businessTypeName = '便捷询价';
      // const businessTypeName: string = '公开招标';
      if (businessTypeName == '便捷询价' || businessTypeName == '便捷磋商' || businessTypeName == '便捷框架协议采购') {
        keys = [...new Set([...noShowKeys, 'source-bidder'])] as String[];
      }
    }
    const tempSteps = newSetpsItems.filter((item) => !keys.includes(item.key));
    setStepsItems(tempSteps);
  }, [noShowKeys]);

  return {
    currentStep,
    changeStep,
    stepsItems,
    setNoShowKeys,
  };
};
